<title>微信自动回复消息表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="Api/WeChatShippingOrder/GetIndex" lay-done="layui.data.done(d);">

</script>
<div class="table-body">
    <table id="LAY-app-WeChatShippingOrder-tableBox" lay-filter="LAY-app-WeChatShippingOrder-tableBox"></table>
    <div id="demo-laypage-pn-show"></div>

</div>


<script type="text/html" id="LAY-app-WeChatShippingOrder-toolbar">
    <div class="layui-form coreshop-toolbar-search-form">
        <div class="layui-form-item">

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="status">
                        <option value="">请选择订单状态</option>
                        {{#  layui.each(indexData.status, function(index, item){ }}
                        <option value="{{item.value}}">{{- item.description}}</option>
                        {{#  }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="LAY-app-WeChatShippingOrder-search"><i class="layui-icon layui-icon-search"></i>筛选</button>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="LAY-app-WeChatShippingOrder-tableBox-bar">

    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">关联订单</a>-->
    {{#  if(d.order_state>1){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">发货详情</a>
    {{#  } }}

    {{#  if(d.order_state==1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="addData">立即发货</a>
    {{#  } else if(d.order_state==2){ }}
    <a class="layui-btn  layui-bg-blue layui-btn-xs" lay-event="updateData">重新发货</a>
    {{#  } }}

</script>



<script>
    var _this = this;
    var indexData;
    var debug = layui.setter.debug;
    var has_more = false;
    var last_index = "";
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coredropdown', 'coreHelper', 'laypage'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , laypage = layui.laypage
                    , view = layui.view;
                var searchwhere;

                //监听搜索
                form.on('submit(LAY-app-WeChatShippingOrder-search)',
                    function (data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reloadData('LAY-app-WeChatShippingOrder-tableBox', { where: field });
                    });
                //数据绑定
                table.render({
                    elem: '#LAY-app-WeChatShippingOrder-tableBox',
                    url: layui.setter.apiUrl + 'Api/WeChatShippingOrder/GetPageList',
                    method: 'POST',
                    where: { last_index: last_index, has_more: has_more },
                    toolbar: '#LAY-app-WeChatShippingOrder-toolbar',
                    //pagebar: '#LAY-app-WeChatShippingOrder-pagebar',
                    className: 'pagebarbox',
                    defaultToolbar: ['filter', 'print', 'exports'],
                    //height: 'full-127',//面包屑142px,搜索框4行172,3行137,2行102,1行67
                    page: false,
                    limit: 10,
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    text: { none: '暂无相关数据' },
                    cols: [
                        [
                            { type: "checkbox", fixed: "left" },
                            //{ field: 'merchant_id', title: '商户号', align: 'center', width: 100, sort: false },
                            //{ field: 'sub_merchant_id', title: '二级商户号', width: 80, sort: false },
                            { field: 'transaction_id', title: '微信订单号', sort: false, width: 205 },
                            { field: 'merchant_trade_no', title: '支付单号', sort: false, width: 125 },
                            {
                                field: 'order_state', title: '发货单状态', sort: false, align: 'center', width: 80, templet: function (data) {
                                    for (var i = 0; i < d.data.status.length; i++) {
                                        if (d.data.status[i].value == data.order_state) {
                                            return d.data.status[i].description;
                                        }
                                    }
                                    return "";
                                }
                            },
                            {
                                field: 'logistics_type', title: '发货模式', sort: false, align: 'center', width: 80, templet: function (data) {
                                    for (var i = 0; i < d.data.logisticsType.length; i++) {
                                        if (d.data.logisticsType[i].value == data.shipping.logistics_type) {
                                            return d.data.logisticsType[i].description;
                                        }
                                    }
                                    return "";
                                }
                            },
                            { field: 'description', title: '发货商品描述', sort: false },
                            {
                                field: 'paid_amount', title: '实际支付', sort: false, align: 'center', width: 80, templet: function (data) {
                                    return '<div>' + (data.paid_amount / 100) + '元' + '</div>';
                                }
                            },
                            { field: 'openid', title: 'openid', sort: false, width: 220 },
                            {
                                field: 'trade_create_time', title: '交易创建时间', sort: false, width: 130, templet: function (data) {
                                    return util.toDateString(data.trade_create_time * 1000, 'yyyy-MM-dd HH:mm:ss');;
                                }
                            },
                            {
                                field: 'pay_time', title: '支付时间', sort: false, width: 130, templet: function (data) {
                                    return util.toDateString(data.pay_time * 1000, 'yyyy-MM-dd HH:mm:ss');
                                }
                            },
                            { field: 'in_complaint', title: '交易纠纷中', width: 85, templet: '#switch_in_complaint', align: 'center', sort: false, unresize: true },

                            { width: 220, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-WeChatShippingOrder-tableBox-bar' }
                        ]
                    ],
                    done: function (res, curr, count, origin) {
                        console.log(res); // 得到当前渲染的数据
                        console.log(curr);  // 得到当前页码
                        console.log(count); // 得到数据总量
                        console.log(res.otherData.has_more);
                        if (res.code == 0) {
                            has_more = res.otherData.has_more;
                            if (res.otherData.has_more) {
                                last_index = res.otherData.last_index;
                            } else {
                                last_index = "";
                            }
                        }

                    },
                });
                //监听排序事件
                table.on('sort(LAY-app-WeChatShippingOrder-tableBox)', function (obj) {
                    table.reloadData('LAY-app-WeChatShippingOrder-tableBox', {
                        initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                        where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                            orderField: obj.field, //排序字段
                            orderDirection: obj.type //排序方式
                        }
                    });
                });

                //监听工具条
                table.on('tool(LAY-app-WeChatShippingOrder-tableBox)',
                    function (obj) {
                        if (obj.event === 'detail') {
                            doDetails(obj);
                        } else if (obj.event === 'updateData') {
                            doUpdate(obj)
                        } else if (obj.event === 'addData') {
                            doCreate(obj)
                        }
                    });
                //执行创建操作
                function doCreate(obj) {
                    coreHelper.Post("Api/WeChatShippingOrder/GetCreate", { data: obj.data.transaction_id, id: obj.data.merchant_trade_no }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '创建数据',
                                area: ['800px', '90%'],
                                id: 'LAY-popup-WeChatShippingOrder-create',
                                success: function (layero, index) {
                                    view(this.id).render('wechatshipping/order/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-WeChatShippingOrder-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.is_all_delivered = field.is_all_delivered == 'on';

                                                var keys = Object.keys(field);
                                                var keysCount = 0;
                                                for (var i = 0; i < keys.length; i++) {
                                                    if (keys[i].indexOf('shipping_list.express_company') != -1) {
                                                        keysCount++;
                                                    }
                                                }
                                                var items = [];
                                                if (keysCount > 0) {
                                                    for (var i = 0; i < keysCount; i++) {
                                                        var item = {};
                                                        item.express_company = field['shipping_list.express_company[' + i + ']'];
                                                        item.tracking_no = field['shipping_list.tracking_no[' + i + ']'];
                                                        items.push(item);
                                                    }
                                                }
                                                field.shipping_list = items;

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/WeChatShippingOrder/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reloadData('LAY-app-WeChatShippingOrder-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                    // 禁止弹窗出现滚动条
                                    //$(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-WeChatShippingOrder-createForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行编辑操作
                function doUpdate(obj) {
                    coreHelper.Post("Api/WeChatShippingOrder/GetUpdate", { data: obj.data.transaction_id, id: obj.data.merchant_trade_no }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '编辑数据',
                                area: ['800px', '90%'],
                                id: 'LAY-popup-WeChatShippingOrder-update',
                                success: function (layero, index) {
                                    view(this.id).render('wechatshipping/order/update', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-WeChatMessageResponse-updateForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.is_all_delivered = field.is_all_delivered == 'on';

                                                var keys = Object.keys(field);
                                                var keysCount = 0;
                                                for (var i = 0; i < keys.length; i++) {
                                                    if (keys[i].indexOf('shipping_list.express_company') != -1) {
                                                        keysCount++;
                                                    }
                                                }
                                                var items = [];
                                                if (keysCount > 0) {
                                                    for (var i = 0; i < keysCount; i++) {
                                                        var item = {};
                                                        item.express_company = field['shipping_list.express_company[' + i + ']'];
                                                        item.tracking_no = field['shipping_list.tracking_no[' + i + ']'];
                                                        items.push(item);
                                                    }
                                                }
                                                field.shipping_list = items;

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/WeChatShippingOrder/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reloadData('LAY-app-WeChatShippingOrder-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                    // 禁止弹窗出现滚动条
                                    //$(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-WeChatMessageResponse-updateForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行预览操作
                function doDetails(obj) {
                    coreHelper.Post("Api/WeChatShippingOrder/GetDetails", { data: obj.data.transaction_id, id: obj.data.merchant_trade_no }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '查看详情',
                                area: ['800px', '600px'],
                                id: 'LAY-popup-WeChatShippingOrder-details',
                                success: function (layero, index) {
                                    view(this.id).render('wechatshipping/order/details', { data: e.data }).done(function () {
                                        form.render();
                                    });
                                    // 禁止弹窗出现滚动条
                                    //$(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }

                // 只显示上一页、下一页、当前页
                laypage.render({
                    elem: 'demo-laypage-pn-show',
                    count: 99999,
                    groups: 1,
                    first: '首页',
                    last: false,
                    layout: ['first', 'page', 'next'],
                    jump: function (obj, first) {
                        // 首次不执行
                        if (!first) {
                            layer.msg('第 ' + obj.curr + ' 页');

                            if (has_more) {
                                table.reloadData('LAY-app-WeChatShippingOrder-tableBox', {
                                    where: { last_index: last_index },
                                    scrollPos: true,
                                });
                            } else {
                                layer.msg('没有更多内容了');

                            }

                            if (obj.curr == 1) {
                                last_index = '';
                                table.reloadData('LAY-app-WeChatShippingOrder-tableBox', {
                                    where: { last_index: '' },
                                    scrollPos: true,
                                });
                            }
                        }
                    }
                });
                //重载form
                form.render();
            });
    };
</script>

<!--设置是否启用-->
<script type="text/html" id="switch_in_complaint">
    <input type="checkbox" name="switch_in_complaint" value="{{d.in_complaint}}" lay-skin="switch" lay-text="是|否" disabled="disabled">
</script>
